import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { StaffsPaginationDto } from './dto/staffs-pagination.dto';
import { FindDataDto } from './dto/find-data.dto';
import { StaffDto } from './dto/staff.dto';
import { CredentialsDto } from './dto/credentials.dto';

@Injectable({
  providedIn: 'root',
})
export class StaffsService {
  constructor(private http: HttpClient) {}

  /**
   * 查询一些员工
   * @param staffsPaginationDto 分页
   */
  find(staffsPaginationDto: StaffsPaginationDto) {
    const { pageIndex, pageSize, keyword } = staffsPaginationDto;
    return this.http.get<FindDataDto<StaffDto>>('staffs', {
      params: {
        pageIndex: pageIndex.toString(),
        pageSize: pageSize.toString(),
        keyword,
      },
    });
  }

  /**
   * 通过员工 _id 删除员工
   * @param _id 员工 _id
   */
  // tslint:disable-next-line: variable-name
  deleteById(_id: string) {
    return this.http.delete(`staffs/${_id}`);
  }

  /**
   * 通过员工 _id 更改密码
   * @param _id 员工 _id
   * @param password 员工的新密码
   */
  // tslint:disable-next-line: variable-name
  updatePassword(_id: string, password: string) {
    return this.http.patch(`staffs/${_id}`, { password });
  }

  /**
   * 创建一个员工
   * @param credentialsDto 员工凭据
   */
  create(credentialsDto: CredentialsDto) {
    return this.http.post('staffs', credentialsDto);
  }
}
